import { combineReducers } from "redux";

var data = {
    goodlist:[],
    collectList: [],
    ahoppingList: [],
    addressList: [],
    cartList: [],
    userInfo: {},
    num: 1
}

function collect_reducer(state = data.collectList, action) {
    var newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case 'COLLECT':
            newstate.push(action.param);
            return newstate;
        case 'CANCEL_COLLECT':
            var i = newstate.findIndex((item) => { return item.id == action.param.id })
            if (i != -1) {
                newstate.splice(i, 1);
            }
            return newstate;
            default:
                return newstate;
        
    }
}
function ahopping_reducer(state = data.ahoppingList, action) {
    var newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case 'AHOPPING':
            newstate.push(action.param);
            return newstate;
        case 'CANCEL_AHOPPING':
            var i = newstate.findIndex((item) => { return item.id == action.param.id })
            if (i != -1) {
                newstate.splice(i, 1);
            }
            return newstate;
            default:
                return newstate;
        
    }
}
function cart_reducer(state = data.cartList, action) {
    var newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        
        case 'ADD':
            return newstate;
        case 'DEL':
            return newstate;
        default:
            return newstate;
    }
}
function address_reducer(state = data.addressList, action) {
    var newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
       
        case 'ADD':
            return newstate;
        case 'DEL':
            return newstate;
        default:
            return newstate;
    }
}
function savelist_reducer(state = data.goodlist, action) {
    var newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        
        case 'SAVE_LIST':
            newstate=action.param
            return newstate;
        
        default:
            return newstate;
    }
}
var rootReducer = combineReducers({
    collectList:collect_reducer,
    ahoppingList:ahopping_reducer,
    cartList:cart_reducer,
    addressList:address_reducer,
    goodlist:savelist_reducer
})
export default rootReducer;